﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_UC_RewritePager : System.Web.UI.UserControl {
    private int _noteTotal = 0; //记录总数
    private int _pageSize = 20; //每页记录数
    private int _pageIndex = 1; //页索引
    private int _pageStep = 5; //步长
    private string _JumpUrl = "?"; //跳转的路径
    private bool _FormatUrl = false; //是否格式化路径(便于Url重写)
    private bool _FormatFix = true; //首页，前页，后页，未页是否包含前缀和后前缀
    private bool _CoverNav = true; //当首页，前页，后页，未页不是链接时是否显示
    private bool _CoverInput = true; //是否包含搜索框
    private string _inputName = "PageIndex"; //文本框名称

    //        private string _indexPrefix = "<td class=\"Tablebody\">&nbsp;"; //链接的前缀
    private string _indexPrefix = ""; //链接的前缀
    //      private string _indexPostfix = "&nbsp;</td>"; //链接的后缀
    private string _indexPostfix = ""; //链接的后缀
    private bool _coverFix = false;  //链接是否包含前缀和后缀

    //自带的两种样式
    private bool _fullStyle = false; //默认样式
    private bool _simpleStyle = false; //默认样式
    private bool _NormalStyle = false;//默认样式

    //string skinpage = Globals.GetSkinPath;
    //private string skinpage = "";
    //首页，前页，后页，未页的显示样式
    private string _linkFirst = "第一页";
    private string _linkLast = "最后一页";
    private string _linkPrev = "&laquo; Prev";
    private string _linkNext = "Next &raquo;";

    //索引样式
    private string _indexStyle = "{0}";
    //显示样式
    //  private string _pageStyle = "<table class=\"Tableborder\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\"><tr height=\"20\"><td class=\"Tableback\">&nbsp;总数：&nbsp;</td><td class=\"Tableback\">&nbsp;每页：{1}&nbsp;</td><td class=\"Tableback\">&nbsp;第{2}/{3}页&nbsp;</td><td class=\"Tableback\">&nbsp;分页：</td>{4}{5}</tr></table>";

    private string _pageStyle = "";
    //   private string _pageStyle = "<ul><li class=\"li_P\">共{0}页&nbsp;每页：{1}&nbsp;&nbsp;第{2}/{3}页&nbsp;分页：</li>{4}<li class=\"li_P marL\">{5}</li></ul>";

    private string _defaultFullStyle = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"page fB\"><tr><td height=\"22\" class=\"padL\">共{0}页&nbsp;每页：{1}&nbsp;&nbsp;第{2}/{3}页&nbsp;分页：{4}{5}</td></tr></table>";

    //      private string _defaultFullStyle = "<table class=\"Tableborder\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\"><tr height=\"20\"><td class=\"Tableback\">&nbsp;总数：{0}&nbsp;</td><td class=\"Tableback\">&nbsp;每页：{1}&nbsp;</td><td class=\"Tableback\">&nbsp;第{2}/{3}页&nbsp;</td><td class=\"Tableback\">&nbsp;分页：</td>{4}{5}</tr></table>";
    private string _defaultSimpleStyle = "<table class=\"Tableborder\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\"><tr><td class=\"Tableback\">&nbsp;{0}&nbsp;</td><td class=\"Tableback\">&nbsp;{1}&nbsp;</td><td class=\"Tableback\">&nbsp;{2}/{3}&nbsp;</td>{4}{5}</tr></table>";

    private string _DefaultNormaiStyle = "第{2}/{3}页&nbsp;&nbsp;{4}";

    private string _DivStyle = @"<div class=""capages  {0}"">{4}</div>";

    private string _CssClass = "";



    protected void Page_Load(object sender, EventArgs e) {
        if (this.PageTotal == 1) { this.Visible = false; return; }
        if (FullStyle) {
            PageStyle = _defaultFullStyle;
        }
        if (SimpleStyle) {
            CoverNav = false;
            PageStyle = _defaultSimpleStyle;
        }

        if (NormalStyle) {
            CoverNav = false;
            PageStyle = _DefaultNormaiStyle;
        }
        if (PageIndex > PageTotal) {
            PageIndex = PageTotal;
        }
        PageStyle = _DivStyle;

        ltNav.Text = string.Format(PageStyle, CssClass, PageSize, PageIndex, PageTotal, PageText, InputText);
    }

    #region 内部方法
    private String FormatToUrl(Int32 pageNo) {
        if (_FormatUrl)
            return string.Format(_JumpUrl, pageNo);
        else
            return JumpUrl + "p=" + pageNo;
    }

    private String FormatToUrl(String pageNo) {
        if (_FormatUrl)
            return string.Format(_JumpUrl, pageNo.ToString());
        else
            return JumpUrl + "p=" + pageNo.ToString();
    }

    private String FormatToFix(String inputVal) {
        if (_FormatFix)
            return _indexPrefix + inputVal + _indexPostfix;
        else
            return inputVal;
    }
    #endregion

    #region 属性
    /// <summary>
    /// 记录总数
    /// </summary>
    public Int32 NoteTotal {
        get { return this._noteTotal; }
        set { this._noteTotal = value; }
    }

    /// <summary>
    /// 页总数
    /// </summary>
    public Int32 PageTotal {
        get {
            int tempPageTotal = NoteTotal % PageSize == 0 ? NoteTotal / PageSize : NoteTotal / PageSize + 1;
            if (tempPageTotal == 0)
                return 1;
            return tempPageTotal;
        }
    }

    /// <summary>
    /// 每页记录数
    /// </summary>
    public Int32 PageSize {
        get { return this._pageSize; }
        set { this._pageSize = value; }
    }

    /// <summary>
    /// 演示
    /// </summary>
    public string CssClass {
        get {
            return _CssClass;
        }
        set {
            if (_CssClass == value)
                return;
            _CssClass = value;
        }
    }

    /// <summary>
    /// 页索引
    /// </summary>
    public Int32 PageIndex {
        get { return this._pageIndex; }
        set { this._pageIndex = value; }
    }

    /// <summary>
    /// 步长
    /// </summary>
    public Int32 PageStep {
        get { return this._pageStep; }
        set { this._pageStep = value; }
    }
    /// <summary>
    /// 链接地址
    /// </summary>
    public String JumpUrl {
        get {
            return _JumpUrl;
            /*  String tempUrl = System.Web.HttpContext.Current.Request.Url.ToString();
              if (tempUrl.IndexOf("?") == -1)
                  return tempUrl + "?";
              else
              {
                  string strp = "";
                  try{
                      strp = System.Web.HttpContext.Current.Request.QueryString["p"];
                  }catch{}
                  if(strp == "")
                      return tempUrl + "&";
                  else
                  {
                      System.Web.HttpContext.Current.Request.Params.Remove("p");
                      return tempUrl + "&";
                  }
              }*/
        }
        set { this._JumpUrl = value; }
    }

    /// <summary>
    /// 是否格式化链接
    /// </summary>
    public Boolean FormatUrl {
        get { return this._FormatUrl; }
        set { this._FormatUrl = value; }
    }

    /// <summary>
    /// 索引前缀
    /// </summary>
    public String IndexPrefix {
        get { return this._indexPrefix; }
        set { this._indexPrefix = value; }
    }

    /// <summary>
    /// 索引后缀
    /// </summary>
    public String IndexPostfix {
        get { return this._indexPostfix; }
        set { this._indexPostfix = value; }
    }

    /// <summary>
    /// 链接是否包含前缀和后缀
    /// </summary>
    public Boolean CoverFix {
        get { return this._coverFix; }
        set { this._coverFix = value; }
    }

    /// <summary>
    /// 首页，前页，后页，未页是否包含前缀和后前缀
    /// </summary>
    public Boolean FormatFix {
        get { return this._FormatFix; }
        set { this._FormatFix = value; }
    }

    /// <summary>
    /// 当首页，前页，后页，未页不为链接时是否显示
    /// </summary>
    public Boolean CoverNav {
        get { return this._CoverNav; }
        set { this._CoverNav = value; }
    }


    /// <summary>
    /// 首页样式
    /// </summary>
    public String LinkFirst {
        get { return this._linkFirst; }
        set { this._linkFirst = value; }
    }

    /// <summary>
    /// 尾页样式
    /// </summary>
    public String LinkLast {
        get { return this._linkLast; }
        set { this._linkLast = value; }
    }

    /// <summary>
    /// 上一页样式
    /// </summary>
    public String LinkPrev {
        get { return this._linkPrev; }
        set { this._linkPrev = value; }
    }

    /// <summary>
    /// 下一页样式
    /// </summary>
    public String LinkNext {
        get { return this._linkNext; }
        set { this._linkNext = value; }
    }

    /// <summary>
    /// 是否显示跳转文本框
    /// </summary>
    public Boolean CoverInput {
        get { return this._CoverInput; }
        set { this._CoverInput = value; }
    }

    /// <summary>
    /// 文本框名称(一页多个用到)
    /// </summary>
    public String InputName {
        get { return this._inputName; }
        set { this._inputName = value; }
    }

    /// <summary>
    /// 跳转的文本框
    /// </summary>
    public String InputText {
        get {
            if (CoverInput)
                return FormatToFix("&nbsp转到：<input type=\"text\" value=\"" + PageIndex + "\" size=\"3\" maxlength=\"10\" name=\"" + InputName + "\" class=\"barInp\">&nbsp;<input type=\"button\" class=\"bGo\" value=\"GO\" onclick=\"window.location.href=\'" + FormatToUrl("\' + this.form." + InputName + ".value + \'") + "\'\">");
            else
                return string.Empty;
        }
    }

    /// <summary>
    /// 显示样式
    /// </summary>
    public String PageStyle {
        get { return this._pageStyle; }
        set { this._pageStyle = value; }
    }

    /// <summary>
    /// 索引样式
    /// </summary>
    public String IndexStyle {
        get { return this._indexStyle; }
        set { this._indexStyle = value; }
    }

    /// <summary>
    /// 全功能样式(自带)
    /// </summary>
    public Boolean FullStyle {
        get { return this._fullStyle; }
        set { this._fullStyle = value; }
    }

    /// <summary>
    /// 简单样式(自带)
    /// </summary>
    public Boolean SimpleStyle {
        get { return this._simpleStyle; }
        set { this._simpleStyle = value; }
    }
    /// <summary>
    /// 普通样式(自带)
    /// </summary>
    public Boolean NormalStyle {
        get { return this._NormalStyle; }
        set { this._NormalStyle = value; }
    }

    /// <summary>
    /// 链接文本
    /// </summary>
    public String PageText {
        get {
            string linkFirstUrl = FormatToFix("<a href=\"" + FormatToUrl(1) + "\">{0}</a>");
            string linkLastUrl = FormatToFix("<a href=\"" + FormatToUrl(PageTotal) + "\">{0}</a>");
            string linkPrevUrl = FormatToFix("<a href=\"" + FormatToUrl(PageIndex - 1) + "\">{0}</a>");
            string linkNextUrl = FormatToFix("<a href=\"" + FormatToUrl(PageIndex + 1) + "\">{0}</a>");

            string linkUrl;
            if (CoverFix)
                linkUrl = "<a href=\"{0}\">" + IndexPrefix + "{1}" + IndexPostfix + "</a>";
            else
                linkUrl = IndexPrefix + "<a href=\"{0}\">{1}</a>" + IndexPostfix + "";

            int pageRoot;
            int pageFoot;
            string pageNav = string.Empty;

            pageRoot = PageIndex - PageStep <= 1 ? 1 : PageIndex - PageStep;
            pageFoot = PageIndex + PageStep >= PageTotal ? PageTotal : PageIndex + PageStep;

            if (PageIndex == 1) {
                if (CoverNav)
                    pageNav += string.Format(@"<span class=""disabled"">{0}</span>", FormatToFix(LinkFirst)) + string.Format(@"<span class=""disabled"">{0}</span>", FormatToFix(LinkPrev));

            }
            else {
              //  pageNav += string.Format(linkFirstUrl, LinkFirst);
                pageNav += string.Format(linkPrevUrl, LinkPrev);
            }

            for (int i = pageRoot; i <= pageFoot; i++) {
                if (i == PageIndex)
                    pageNav += @"<strong>" + FormatToFix(string.Format(IndexStyle, i.ToString())) + "</strong>";
                else
                    pageNav += string.Format(linkUrl, FormatToUrl(i), i.ToString());

                if (i == PageTotal)
                    break;
            }

            if (PageIndex == PageTotal) {
                if (CoverNav)
                    pageNav += string.Format(@"<span class=""disabled"">{0}</span>", FormatToFix(LinkNext)) + string.Format(@"<span class=""disabled"">{0}</span>", FormatToFix(LinkLast));
            }
            else {
                pageNav += string.Format(linkNextUrl, LinkNext);
                //pageNav += string.Format(linkLastUrl, LinkLast);
            }
            return pageNav;
        }
    }
    #endregion
}